//
//  DCTabBarController.m
//  商城
//
//  Created by runone02 on 2018/8/27.
//  Copyright © 2018年 liqiyun. All rights reserved.
//

#import "DCTabBarController.h"
#import "DCNavigationController.h"
#import "DCLoginViewController.h"
#import "DCBaseSetViewController.h"

#import "DCTabBadgeView.h"

#import "DCHandPickViewController.h"
#import "DCMediaListViewController.h"
#import "DCBeautyShopViewController.h"
#import "DCBeautyMessageViewController.h"
#import "DCMyCenterViewController.h"

@interface DCTabBarController ()

@end

@implementation DCTabBarController

- (void)viewDidLoad {
    [super viewDidLoad];
    [self addChildVC];
    
}



- (void) addChildVC {
    DCBeautyMessageViewController *message = [[DCBeautyMessageViewController alloc]init];
    [self addChildVC:message title:@"消息" norImage:@"tabbar_icon_message" highlightImage:@"tabbar_icon_message_s"];
    
    DCHandPickViewController * fate = [[DCHandPickViewController alloc]init];
    [self addChildVC:fate title:@"首页" norImage:@"tabbar_icon0" highlightImage:@"tabbar_icon0_s"];
    
    DCMediaListViewController * letter = [[DCMediaListViewController alloc]init];
    [self addChildVC:letter title:@"美榜" norImage:@"tabbar_icon1" highlightImage:@"tabbar_icon1_s"];

    DCBeautyShopViewController *multi = [[DCBeautyShopViewController alloc]init];
    [self addChildVC:multi title:@"店铺" norImage:@"tabbar_icon2" highlightImage:@"tabbar_icon2_s"];
    
    DCMyCenterViewController *mine = [[DCMyCenterViewController alloc]init];
    [self addChildVC:mine  title:@"我的" norImage:@"tabbar_mine" highlightImage:@"tabbar_mine_select"];
}



/**
 添加子控制器
 
 @param chinldVC 子控制器
 @param title 子控制器标题
 @param norImage 默认状态下图片
 @param highlightImage 高亮状态下图片
 */
- (void) addChildVC : (UIViewController *) chinldVC title: (NSString *)title norImage: (NSString *)norImage highlightImage: (NSString *) highlightImage{
    
    //设置子控制器标题
//    chinldVC.navigationItem.title = title;
    chinldVC.tabBarItem.title = title;
    
    //设置tabbar按钮图片
    [chinldVC.tabBarItem setImage:[UIImage imageNamed:norImage]];
    [chinldVC.tabBarItem setSelectedImage:[UIImage imageNamed:highlightImage]];
    
    //导航控制器
    DCNavigationController *nav = [[DCNavigationController alloc]initWithRootViewController:chinldVC];
    
    [self addChildViewController:nav];
}

/**  */
- (void)tabBar:(UITabBar *)tabBar didSelectItem:(UITabBarItem *)item{
    NSInteger index = [self.tabBar.items indexOfObject:item];
    [self playSound];
    [self animationWhithIndex:index];
}


/**
 点击时的声效
 */
- (void) playSound {
    //    NSString *path = [[NSBundle mainBundle] pathForResource:@"like" ofType:@"caf"];
    //
    //    SystemSoundID soundID;
    
    //    NSURL *soundURL = [NSURL fileURLWithPath:path];
    //    AudioServicesCreateSystemSoundID((__bridge CFURLRef)soundURL,&soundID);
    //    AudioServicesPlaySystemSound(soundID);
}

- (void)animationWhithIndex:(NSInteger) index{
    if (self.selectedIndex == index) {
        return;
    }
    NSMutableArray *tabbarbuttonArray = [NSMutableArray array];
    for (UIView *tabBarButton in self.tabBar.subviews) {
        if ([tabBarButton isKindOfClass:NSClassFromString(@"UITabBarButton")]) {
            [tabbarbuttonArray addObject:tabBarButton];
        }
    }
    CATransition *animtion = [CATransition animation];
    [animtion setDuration:0.3f];
    
    //切换动画风格
    [animtion setType:kCATransitionFade];
    
    //动画切换方向
    [animtion setSubtype:kCATransitionFromBottom];
    [animtion setTimingFunction:[CAMediaTimingFunction functionWithName:kCAMediaTimingFunctionEaseInEaseOut]];
    
    [[tabbarbuttonArray[index] layer]addAnimation:animtion forKey:@"UITabBarButton.transform.scale"];
}

@end
